package com.nfc;

import java.io.IOException;
import java.nio.ByteBuffer;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.jb.Preference;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.MifareUltralight;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.nfc.tech.NfcA;
import android.nfc.tech.NfcB;
import android.nfc.tech.NfcF;
import android.nfc.tech.NfcV;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.ge.test.NdkarrayActivity;

import com.example.ht380d4G_jar_demo.R;

public class NFCActivity_Houtai extends Activity {
	// boolean bind = false;
	// private ServiceConnection serviceConnection;
	// private NFCService nfcService;
	// private static final String FROM_TAG = "MainActivity";
	// 标识是否文本框显示
	// private boolean isHoutai = false;
	private NfcAdapter nfcAdapter;
	public static Intent intent;
	private PendingIntent mPendingIntent;
	private boolean canWrite = true;
	private int cardType = CardType.unknow;
	private TextView cardTypeTv, cardNumTv, cardInfoTv, dataReadTv;
	private Spinner blockS;
	private EditText keyEt, dataWriteEt;
	private static final String[] blocks = new String[] { "0", "1", "2" };
	private byte[] key = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
			(byte) 0xff, (byte) 0xff };

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1000:
				Tag tag = (Tag) msg.obj;
				parserData(tag);
				break;

			case 1001:

				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		// Intent it = getIntent();
		// if (it.hasExtra("from")) {
		// String from = it.getStringExtra("from");
		// if (from.equals(FROM_TAG)) {
		// isHoutai = false;
		// }
		// }

		super.onCreate(savedInstanceState);
		setContentView(R.layout.nfc);
		initView();

		nfcAdapter = NfcAdapter.getDefaultAdapter(this);
		if (nfcAdapter == null) {
			Toast.makeText(this, getString(R.string.nfc_device_not_support),
					Toast.LENGTH_SHORT).show();
			finish();
		}
		if (nfcAdapter != null &&!nfcAdapter.isEnabled()) {
			AlertDialog dialog = new AlertDialog.Builder(this)
					.setMessage(getString(R.string.nfc_not_open_go_open))
					.setPositiveButton(getString(R.string.yes),
							new OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									Intent intent = new Intent(
											"android.settings.NFC_SETTINGS");
									startActivityForResult(intent, 100);
								}
							}).create();
			dialog.show();
		}
		mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
				getClass()), 0);
		// Intent service = new Intent(this, NFCService.class);
		// startService(service);
		// serviceConnection = new ServiceConnection() {
		//
		// @Override
		// public void onServiceDisconnected(ComponentName name) {
		// bind = false;
		// }
		//
		// @Override
		// public void onServiceConnected(ComponentName name, IBinder service) {
		// bind = true;
		// NFCBinder myBinder = (NFCBinder) service;
		// nfcService = myBinder.getService();
		// Tag tag = getIntent().getParcelableExtra(nfcAdapter.EXTRA_TAG);
		// if(bind){
		// nfcService.parserData(tag);
		// }
		// }
		// };
		// bindService(service, serviceConnection, BIND_AUTO_CREATE);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// if (bind) {
		// unbindService(serviceConnection);
		// }
	}

	private void initView() {
		// TODO Auto-generated method stub
		cardTypeTv = (TextView) findViewById(R.id.nfc_cardtype_tv);
		cardNumTv = (TextView) findViewById(R.id.nfc_cardnumber_tv);
		cardInfoTv = (TextView) findViewById(R.id.nfc_cardinfo_tv);
		dataReadTv = (TextView) findViewById(R.id.nfc_data_reaed_tv);
		blockS = (Spinner) findViewById(R.id.nfc_fan_sp);
		keyEt = (EditText) findViewById(R.id.nfc_section_key_et);
		dataWriteEt = (EditText) findViewById(R.id.nfc_data_write_ev);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (nfcAdapter != null) {
			nfcAdapter.enableForegroundDispatch(this, mPendingIntent, null,
					null);
			Tag tag = getIntent().getParcelableExtra(nfcAdapter.EXTRA_TAG);
			// if (isHoutai) {
			Log.v("NFCActivity", "is Houtai");
			finish();
			PostBrocast(tag);
			// } else {
			// Log.v("NFCActivity", "is not Houtai");
			// parserData(tag);
			// }
		}
	}

	/**
	 * 用于后台文本框显示
	 */
	private void PostBrocast(Tag tag) {
		String str = null;
		if (tag == null)
			return;
		// final byte[] id = tag.getId();
		// String[] techList = tag.getTechList();
		// if (techList != null && techList.length > 0) {
		// if (techList[0].contains("NfcV")) {
		// Card.mNfcV = NfcV.get(tag);
		// cardType = CardType.NfcV;
		// }
		// }
		str = bytesToHexString(tag.getId());
		// 发送广播
		if(Preference.getNfcBackgroundSupport(this, true)){
			if(!Preference.getNfcSimulateKeySupport(this, false)){
				Intent editintent = new Intent("com.haier.EDITTEXT");
				editintent.putExtra("showvalue", str + "\n");
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				this.sendBroadcast(editintent);
				Log.e("editintent", str);
			}else {
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				simulatekey(str);
			}
			
		}
	}

	// @Override
	// protected void onNewIntent(Intent intent) {
	// // TODO Auto-generated method stub
	// Tag tag = intent.getParcelableExtra(nfcAdapter.EXTRA_TAG);
	//
	// if (isHoutai) {
	// finish();
	// PostBrocast(tag);
	// } else {
	// parserData(tag);
	// }
	// }

	private void parserData(Tag tag) {

		if (tag == null)
			return;
		final byte[] id = tag.getId();
		String str = bytesToHexString(tag.getId());
		// 发送广播
		if(Preference.getNfcBackgroundSupport(this, true)){
			if(Preference.getNfcSimulateKeySupport(this, false)){
				Intent editintent = new Intent("com.haier.EDITTEXT");
				editintent.putExtra("showvalue", str + "\n");
				this.sendBroadcast(editintent);
			}else {
				simulatekey(str);
			}

		}
		Log.e("editintent", str);
		if (cardNumTv != null)
			cardNumTv.setText(str);

		if (dataReadTv != null) {
			dataReadTv.setText("");
		}
		String[] techList = tag.getTechList();

		if (techList != null && techList.length > 0) {

			if (cardTypeTv != null) {
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < techList.length; i++) {

					sb.append((i + 1)
							+ " "
							+ techList[i].subSequence(
									techList[i].lastIndexOf(".") + 1,
									techList[i].length()));
					sb.append("\n");
				}
				cardTypeTv.setText(sb.subSequence(0, sb.length() - 1));

			}

			if (techList[0].contains("MifareClassic")) {
				Card.mMifareClassic = MifareClassic.get(tag);
				cardType = CardType.MifareClassic;
				dataReadTv.setText(getString(R.string.nfc_block_count)
						+ Card.mMifareClassic.getBlockCount() + "\n"
						+ getString(R.string.nfc_max_command_len)
						+ Card.mMifareClassic.getMaxTransceiveLength());
			} else if (techList[0].contains("MifareUltralight")) {
				Card.mMifareUltralight = MifareUltralight.get(tag);
				cardType = CardType.MifareUltralight;
				dataReadTv.setText("\n"
						+ getString(R.string.nfc_max_command_len)
						+ Card.mMifareUltralight.getMaxTransceiveLength());
			} else if (techList[0].contains("NdefFormatable")) {
				Card.mNdefFormatable = NdefFormatable.get(tag);
				cardType = CardType.NdefFormatable;
			} else if (techList[0].contains("Ndef")) {
				Card.mNdef = Ndef.get(tag);
				cardType = CardType.Ndef;
				// Card.mNfcF.transceive(data)
			} else if (techList[0].contains("NfcV")) {
				Card.mNfcV = NfcV.get(tag);
				cardType = CardType.NfcV;
				dataReadTv
						.setText("DSF ID:"
								+ bytesToHexString(new byte[] { Card.mNfcV
										.getDsfId() }));
			} else if (techList[0].contains("NfcF")) {
				Card.mNfcF = NfcF.get(tag);
				cardType = CardType.NfcF;
				try {
					dataReadTv
							.setText(getString(R.string.nfc_manu)
									+ new String(Card.mNfcF.getManufacturer(),
											"GB2312") + "\n"
									+ getString(R.string.nfc_max_command_len)
									+ Card.mNfcF.getMaxTransceiveLength());
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}

			} else if (techList[0].contains("NfcB")) {
				Card.mNfcB = NfcB.get(tag);
				cardType = CardType.NfcB;
				try {
					dataReadTv
							.setText(getString(R.string.nfc_prof)
									+ new String(Card.mNfcB.getProtocolInfo(),
											"GB2312") + "\n"
									+ getString(R.string.nfc_max_command_len)
									+ Card.mNfcB.getMaxTransceiveLength());
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			} else if (techList[0].contains("NfcA")) {
				Card.mNfcA = NfcA.get(tag);
				cardType = CardType.NfcA;
				dataReadTv.setText("ATQA:"
						+ bytesToHexString(Card.mNfcA.getAtqa()) + "\nSak:"
						+ Card.mNfcA.getSak() + "\n"
						+ getString(R.string.nfc_max_command_len)
						+ Card.mNfcA.getMaxTransceiveLength());

				try {
					Card.mNfcA.connect();
					Card.mNfcA.transceive(new byte[] { 0x00 });
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else if (techList[0].contains("IsoDep")) {
				Card.mIsoDep = IsoDep.get(tag);
				cardType = CardType.IsoDep;
				StringBuilder sb = new StringBuilder();
				sb.append(getString(R.string.nfc_max_command_len)
						+ Card.mIsoDep.getMaxTransceiveLength());
				try {
					Card.mIsoDep.connect();
					if (Card.mIsoDep.isConnected()) {

						// select the card manager applet
						byte[] mf = { (byte) '1', (byte) 'P', (byte) 'A',
								(byte) 'Y', (byte) '.', (byte) 'S', (byte) 'Y',
								(byte) 'S', (byte) '.', (byte) 'D', (byte) 'D',
								(byte) 'F', (byte) '0', (byte) '1', };
						byte[] mfRsp = Card.mIsoDep
								.transceive(getSelectCommand(mf));
						Log.d("test", "mfRsp:" + bytesToHexString(mfRsp));
						// select Main Application
						byte[] szt = { (byte) 'P', (byte) 'A', (byte) 'Y',
								(byte) '.', (byte) 'S', (byte) 'Z', (byte) 'T' };
						byte[] sztRsp = Card.mIsoDep
								.transceive(getSelectCommand(szt));
						Log.d("test", "sztRsp:" + bytesToHexString(sztRsp));

						byte[] balance = { (byte) 0x80, (byte) 0x5C, 0x00,
								0x02, 0x04 };
						byte[] balanceRsp = Card.mIsoDep.transceive(balance);
						Log.d("test", "balanceRsp:"
								+ bytesToHexString(balanceRsp));
						if (balanceRsp != null && balanceRsp.length > 4) {
							int cash = byteToInt(balanceRsp, 4);
							float ba = cash / 100.0f;
							if (dataReadTv != null) {
								sb.append("\n" + "余额:" + ba);
							}
						}
					}
				} catch (IOException e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				dataReadTv.setText(sb.toString());
			} else if (techList[0].contains("NfcBarcode")) {
				// Card.mNfcBarcode = NfcBarcode.get(tag);
				// cardType = CardType.NfcBarcode;
				cardType = CardType.unknow;
			} else {
				cardType = CardType.unknow;
			}
		}
		// if (!haveMifareUltralight) {
		// Toast.makeText(this, "不支持MifareClassic", Toast.LENGTH_LONG).show();
		// return;
		// }
		// if (canWrite) {
		// writeTag(tag);
		// } else {
		// String data = readTag(tag);
		// if (data != null) {
		// Log.i(data, "ouput");
		// Toast.makeText(this, data, Toast.LENGTH_LONG).show();
		// }
		// }

		// NFCActivity.this.finish();
	}

	private byte[] getSelectCommand(byte[] aid) {
		final ByteBuffer cmd_pse = ByteBuffer.allocate(aid.length + 6);
		cmd_pse.put((byte) 0x00) // CLA Class
				.put((byte) 0xA4) // INS Instruction
				.put((byte) 0x04) // P1 Parameter 1
				.put((byte) 0x00) // P2 Parameter 2
				.put((byte) aid.length) // Lc
				.put(aid).put((byte) 0x00); // Le
		return cmd_pse.array();
	}

	private int byteToInt(byte[] b, int n) {
		int ret = 0;
		for (int i = 0; i < n; i++) {
			ret = ret << 8;
			ret |= b[i] & 0x00FF;
		}
		if (ret > 100000 || ret < -100000)
			ret -= 0x80000000;
		return ret;
	}

	public String readTag(Tag tag) {
		MifareClassic mfc = MifareClassic.get(tag);
		for (String tech : tag.getTechList()) {
			System.out.println(tech);
		}
		boolean auth = false;
		// 读取TAG

		try {
			String metaInfo = "";
			// Enable I/O operations to the tag from this TagTechnology object.
			mfc.connect();
			int type = mfc.getType();// 获取TAG的类型
			int sectorCount = mfc.getSectorCount();// 获取TAG中包含的扇区数
			String typeS = "";
			switch (type) {
			case MifareClassic.TYPE_CLASSIC:
				typeS = "TYPE_CLASSIC";
				break;
			case MifareClassic.TYPE_PLUS:
				typeS = "TYPE_PLUS";
				break;
			case MifareClassic.TYPE_PRO:
				typeS = "TYPE_PRO";
				break;
			case MifareClassic.TYPE_UNKNOWN:
				typeS = "TYPE_UNKNOWN";
				break;
			}
			metaInfo += "卡片类型：" + typeS + "\n共" + sectorCount + "个扇区\n共"
					+ mfc.getBlockCount() + "个块\n存储空间: " + mfc.getSize()
					+ "B\n";
			for (int j = 0; j < sectorCount; j++) {
				// Authenticate a sector with key A.
				auth = mfc.authenticateSectorWithKeyA(j,
						MifareClassic.KEY_NFC_FORUM);
				int bCount;
				int bIndex;
				if (auth) {
					metaInfo += "Sector " + j + ":验证成功\n";
					// 读取扇区中的块
					bCount = mfc.getBlockCountInSector(j);
					bIndex = mfc.sectorToBlock(j);
					for (int i = 0; i < bCount; i++) {
						byte[] data = mfc.readBlock(bIndex);
						metaInfo += "Block " + bIndex + " : "
								+ bytesToHexString(data) + "\n";
						bIndex++;
					}
				} else {
					metaInfo += "Sector " + j + ":验证失败\n";
				}
			}
			return metaInfo;
		} catch (Exception e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
			e.printStackTrace();
		} finally {
			if (mfc != null) {
				try {
					mfc.close();
				} catch (IOException e) {
					Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			}
		}
		return null;

	}

	// 字符序列转换为16进制字符串
	private String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		char[] buffer = new char[2];
		for (int i = 0; i < src.length; i++) {
			buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);
			buffer[1] = Character.forDigit(src[i] & 0x0F, 16);
			System.out.println(buffer);
			stringBuilder.append(buffer);
		}
		return stringBuilder.toString();
	}

	public void writeTag(Tag tag) {

		MifareClassic mfc = MifareClassic.get(tag);

		try {
			mfc.connect();
			boolean auth = false;
			short sectorAddress = 1;
			auth = mfc.authenticateSectorWithKeyA(sectorAddress,
					MifareClassic.KEY_NFC_FORUM);
			if (auth) {
				// the last block of the sector is used for KeyA and KeyB cannot
				// be overwritted
				mfc.writeBlock(4, "1313838438000000".getBytes());
				mfc.writeBlock(5, "1322676888000000".getBytes());
				mfc.close();
				Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				mfc.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (nfcAdapter != null) {
			nfcAdapter.disableForegroundDispatch(this);
		}
	}

	// @Override
	// protected void onActivityResult(int requestCode, int resultCode, Intent
	// data) {
	// // TODO Auto-generated method stub
	// super.onActivityResult(requestCode, resultCode, data);
	// if (requestCode == 100) {
	// nfcAdapter = NfcAdapter.getDefaultAdapter(this);
	// if (!nfcAdapter.isEnabled()) {
	// Toast.makeText(this, getString(R.string.nfc_not_open),
	// Toast.LENGTH_SHORT).show();
	// }
	// }
	// }

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	/**
	 * 模拟键盘输出文本
	 * @param data 数据 
	 */
	public void simulatekey(String data) {
		// TODO Auto-generated method stub
		if (data != null && data.length() > 0) {
			for (int i = 0; i < data.length(); i++) {
				converChar(data.charAt(i));
			}
			
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 28 });
			System.out.println("simulatekey");
		}
	}
	
	private void converChar(char charAt) {
		// TODO Auto-generated method stub
		switch (charAt) {
		case 27:// esc
			NdkarrayActivity.simulatekeyWithValue(42, 1);// 抬起shift
			NdkarrayActivity.simulatekey(new byte[] { (byte) 1 });
			break;
		case '1':// 1
			NdkarrayActivity.simulatekeyWithValue(42, 1);// 抬起shift
			NdkarrayActivity.simulatekey(new byte[] { (byte) 2 });
			break;
		case '!':
			NdkarrayActivity.simulatekeyWithValue(42, 0);// 按下shift
			NdkarrayActivity.simulatekey(new byte[] { (byte) 2 });
			break;
		case '2':// 2
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 3 });
			break;
		case '@':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 3 });
			break;
		case '3':// 3
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 4 });
			break;
		case '#':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 4 });
			break;
		case '4'://
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 5 });
			break;
		case '$':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 5 });
			break;
		case '5':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 6 });
			break;
		case '%':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 6 });
			break;
		case '6':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 7 });
			break;
		case '^':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 7 });
			break;
		case '7':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 8 });
			break;
		case '&':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 8 });
			break;
		case '8':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 9 });
			break;
		case '*':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 9 });
			break;
		case '9':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 10 });
			break;
		case '(':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 10 });
			break;
		case '0':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 11 });
			break;
		case ')':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 11 });
			break;
		case '-':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 12 });
			break;
		case '_':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 12 });
			break;
		case '=':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 13 });
			break;
		case '+':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 13 });
			break;
		case 127:
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 14 });
			break;
		case 9:
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 15 });
			break;
		case 'q':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 16 });
			break;
		case 'Q':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 16 });
			break;
		case 'w':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 17 });
			break;
		case 'W':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 17 });
			break;
		case 'e':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 18 });
			break;
		case 'E':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 18 });
			break;
		case 'r':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 19 });
			break;
		case 'R':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 19 });
			break;
		case 't':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 20 });
			break;
		case 'T':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 20 });
			break;
		case 'y':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 21 });
			break;
		case 'Y':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 21 });
			break;
		case 'u':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 22 });
			break;
		case 'U':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 22 });
			break;
		case 'i':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 23 });
			break;
		case 'I':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 23 });
			break;
		case 'o':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 24 });
			break;
		case 'O':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 24 });
			break;
		case 'p':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 25 });
			break;
		case 'P':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 25 });
			break;
		case '[':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 26 });
			break;
		case '{':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 26 });
			break;
		case ']':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 27 });
			break;
		case '}':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 27 });
			break;
		case 13:
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 28 });
			break;
		case 'a':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 30 });
			break;
		case 'A':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 30 });
			break;
		case 's':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 31 });
			break;
		case 'S':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 31 });
			break;
		case 'd':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 32 });
			break;
		case 'D':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 32 });
			break;
		case 'f':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 33 });
			break;
		case 'F':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 33 });
			break;
		case 'g':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 34 });
			break;
		case 'G':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 34 });
			break;
		case 'h':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 35 });
			break;
		case 'H':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 35 });
			break;
		case 'j':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 36 });
			break;
		case 'J':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 36 });
			break;
		case 'k':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 37 });
			break;
		case 'K':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 37 });
			break;
		case 'l':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 38 });
			break;
		case 'L':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 38 });
			break;
		case ';':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 39 });
			break;
		case ':':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 39 });
			break;
		case '\'':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 40 });
			break;
		case '"':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 40 });
			break;
		case 'z':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 44 });
			break;
		case 'Z':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 44 });
			break;
		case 'x':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 45 });
			break;
		case 'X':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 45 });
			break;
		case 'c':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 46 });
			break;
		case 'C':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 46 });
			break;
		case 'v':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 47 });
			break;
		case 'V':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 47 });
			break;
		case 'b':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 48 });
			break;
		case 'B':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 48 });
			break;
		case 'n':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 49 });
			break;
		case 'N':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 49 });
			break;
		case 'm':
			NdkarrayActivity.simulatekeyWithValue(42, 1);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 50 });
			break;
		case 'M':
			NdkarrayActivity.simulatekeyWithValue(42, 0);
			NdkarrayActivity.simulatekey(new byte[] { (byte) 50 });
			break;
		default:
			break;
		}
	}
}
